home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7568 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: HELP: Illegal Pointer Arithmetic
  5. Date: Tue, 27 Feb 1996 14:31:04 GMT
  6. Organization: Netcom
  7. Message-ID: <31331425.52605482@nntp.ix.netcom.com>
  8. References: <4gj0ug$730@news.one.net> <danpop.825121139@rscernix> <31306a18.309961701@nntp.ix.netcom.com> <4gq77b$bmo@reznor.larc.nasa.gov> <4gqija$bmo@reznor.larc.nasa.gov>
  9. NNTP-Posting-Host: ix-dc12-08.ix.netcom.com
  10. X-NETCOM-Date: Tue Feb 27  6:30:55 AM PST 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. hook@cscsun3.larc.nasa.gov (Ed Hook) wrote:
  14.  
  15. >  Following up to myself:
  16. > In article <4gq77b$bmo@reznor.larc.nasa.gov>, hook@cscsun3.larc.nasa.gov (Ed Hook) writes:
  17. > |> In article <31306a18.309961701@nntp.ix.netcom.com>, miker3@ix.netcom.com (Mike Rubenstein) writes:
  18. >        [ *deletia* ]
  19. > |> |> 
  20. > |> |> Not quite that, but Shildt's Annotated ANSI C Standard says
  21. > |> |> 
  22. > |> |>     The following fragment illustrates how files are commonly 
  23. > |> |>     read:
  24. > |> |> 
  25. > |> |>         do {
  26. > |> |>           ch = fgetc(fp);
  27. > |> |>           /* ... */
  28. > |> |>         } while (!feof(fp));
  29. > |> |> 
  30. > |> |> Come on Dan.  You're slipping.  Surely you could have guessed who
  31. > |> |> teaches this idiocy.
  32. > |> |>
  33. > |>    It depresses me to have to defend Schildt, but that loop actually
  34. > |>    works as advertised. (I *do* question his use of "commonly", since
  35. > |>    I have encountered _any_ code that does it that way ... .) Anyhow,
  36. > |>    since 'feof()' is called *after* each iteration of the loopbody,
  37. > |>    the loop terminates precisely after the last character is fetched
  38. > |>    from the file.
  39. > |>
  40. >   Blair Houghton gently pointed out to me (_via_ e-mail) that the above
  41. >   defense of Schildt is *erroneous*, since the code in question fails on
  42. >   an _empty_ input file. Overall, I feel somehow _cleaner_ now ...
  43.  
  44. Actually, it fails on every input file since it goes through the loop
  45. after reaching EOF.  An empty file is just one example.  On a one
  46. character file it goes through the loop twice, once with the character
  47. and once with EOF.
  48.  
  49. Unless, of course, the unshown code includes a test for ch == EOF, but
  50. that would be contrary to the concept of an example.  feof() returns
  51. nonzero only after end of file has been reached.
  52.  
  53.  
  54. Michael M Rubenstein
  55.